﻿Imports System.Data.SqlClient
Imports DTO
Public Class HoaDonDAO
    Dim dp As New DataProvider

    Public Function LoadDsHD() As DataTable
        Dim kh As New DataTable
        kh = dp.ExecuteQuery("LoadHD", Nothing)
        Return kh
    End Function

    Public Function LAYTHEOID(ByVal id As Integer) As HoaDonDTO
        Dim cn As New DataProvider()
        Dim para = New SqlParameter("@MaHD", id)
        Dim dt = cn.ExecuteQuery("LoadHDID", para)
        Dim hoadon As New HoaDonDTO()
        If dt.Rows.Count <> 0 Then
            hoadon.MaHD = Integer.Parse(dt.Rows(0).Item("MaHD").ToString())
            hoadon.MaSP = Integer.Parse(dt.Rows(0).Item("MaSP").ToString())
            hoadon.NgayMua = DateTime.Parse(dt.Rows(0).Item("NgayLap"))
            hoadon.SoLuong = Integer.Parse(dt.Rows(0).Item("SoLuong").ToString())
            hoadon.TongTien = Integer.Parse(dt.Rows(0).Item("TongTien").ToString())
            hoadon.DonGia = Integer.Parse(dt.Rows(0).Item("DonGia").ToString())
        End If
        Return hoadon
    End Function

   

    Public Function layTatCa() As IList
        Dim cn As New DataProvider()
        Dim array As New ArrayList
        Dim dt = cn.ExecuteQuery("LayTaTCaHD")

        If dt.Rows.Count <> 0 Then
            For Each dr As DataRow In dt.Rows
                Dim hoadon As New HoaDonDTO()
                hoadon.MaHD = Integer.Parse(dr.Item("MaHD").ToString())
                hoadon.MaSP = Integer.Parse(dr.Item("MaSP").ToString())
                hoadon.NgayMua = DateTime.Parse(dr.Item("NgayLap").ToString())
                hoadon.SoLuong = Integer.Parse(dr.Item("SoLuong").ToString())
                hoadon.MaNV = Integer.Parse(dr.Item("MaNV").ToString())
                hoadon.TongTien = Integer.Parse(dr.Item("TongTien").ToString())
                hoadon.DonGia = Integer.Parse(dr.Item("DonGia").ToString())
                array.Add(hoadon)
            Next
        End If
        Return array
    End Function
    Public Sub Them(ByVal hd As HoaDonDTO)

        Dim dt As New DataProvider()
        Dim para As SqlParameter() = New SqlParameter(5) {}
        para(0) = New SqlParameter("@MaSP", hd.MaSP)
        para(1) = New SqlParameter("@ngaylap", hd.NgayMua)
        para(2) = New SqlParameter("@SoLuong", hd.SoLuong)
        para(3) = New SqlParameter("@MaNV", hd.MaNV)
        para(4) = New SqlParameter("@TongTien", hd.TongTien)
        para(5) = New SqlParameter("@DonGia", hd.DonGia)
        dt.ExecuteNonQuery("InsertHD", para)

    End Sub

    Public Sub Xoa(ByVal id As Integer)
        Dim cn As New DataProvider()
        Dim para = New SqlParameter("@MaHD", id)
        cn.ExecuteNonQuery("DeleteHD", para)
    End Sub

    Function layTatCaTB() As DataTable
        Dim cn As New DataProvider()
        Return cn.ExecuteQuery("LayTaTCaHD")
    End Function

    Function DoanhThu(ByVal ngaydau As Date, ByVal ngaycuoi As Date) As DataTable
        Dim dp As New DataProvider
        Dim dt As New DataTable
        Dim para As SqlParameter() = New SqlParameter(1) {}
        para(0) = New SqlParameter("@ngaybatdau", ngaydau)
        para(1) = New SqlParameter("@ngayketthuc", ngaycuoi)
        dt = dp.ExecuteQuery("Repost", para)
        Return dt

    End Function

    Function LayTatCaTT() As DataTable
        Dim cn As New DataProvider()
        Dim para As New SqlParameter()
        Return cn.ExecuteQuery("LoadHDTheoID")
    End Function



 
  

    
End Class
